/**
 * 
 * @author  phoenics@126.com
 * @version v.0.1
 * @date  2018年3月15日 上午8:59:05 
 * 
 */
package phoenics.upservice.restcontroller;

import java.util.Optional;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import phoenics.common.BaseData.RestResp;
import phoenics.upservice.auth.JwtAuthentication;
import phoenics.upservice.dto.MobileToken;
import phoenics.upservice.dto.NameAndPass;
import phoenics.upservice.entity.UserToken;
import phoenics.upservice.entity.User;
import phoenics.upservice.repository.UserRepo;
import phoenics.upservice.repository.UserTokenRepo;
import phoenics.upservice.service.JwtService;

/**
 *
 */
@Api(value = "与Token有关的API")
@RestController  
@RequestMapping("/token")  
public class TokenController {
	private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(TokenController.class);
	@Autowired
	BCryptPasswordEncoder passwordEncoder;
	@Resource  
    private UserRepo userRepo;  
    @Resource  
    private JwtService jwtService;  
    @Resource  
    private UserTokenRepo userTokenRepo;  
    @ApiOperation(value = "enroll", notes = "")  
    @PostMapping  (value = "/enroll")
    public RestResp enroll(@RequestBody NameAndPass user){  
       return userRepo.findByMobile(user.getUsername()).map(u -> {  
           UserToken userToken = new UserToken(jwtService.generate(u));  
           logger.info("{} enrolled", user.getUsername());  
           userToken = userTokenRepo.save(userToken);  
           return RestResp.success(userToken);  
        }).orElse(RestResp.fail());  
  
    } 
    @ApiOperation(value = "登录取得token", notes = "")  
    @PostMapping  (value = "/gtoken")
    public RestResp takeToken(@RequestBody NameAndPass user){  
    	//String password=md5encoder.encodePassword(user.getPassword(), user.getUsername());
    	logger.info("password={}  password1={}    name={}",passwordEncoder.encode(user.getPassword()),user.getPassword(),user.getUsername());
    	return userRepo.findByMobile(user.getUsername()).map(u->{
    		if(passwordEncoder.matches(u.getPassword(), passwordEncoder.encode(user.getPassword()))) {
    			return RestResp.fail("密码错误");
    		}
    		MobileToken mobileToken = new MobileToken(jwtService.generate(u));  
    		 return RestResp.success(mobileToken);  
    	}).orElse(RestResp.fail("手机号错误"));
       /* return userRepo.findByMobileAndPassword(user.getUsername(), user.getPassword()).map(u -> {  
           UserToken userToken = new UserToken(jwtService.generate(u));  
           log.info("{} enrolled", user.getUsername());  
           userToken = userTokenRepo.save(userToken);  
           return RestResp.success(userToken);  
        }).orElse(RestResp.fail());  */
  
    }  
}
